<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@include file="/header.jsp" %>
<script type="text/javascript">
    function validateRadio(formId,raName,alt) {
        var val=$("form[id='"+formId+"'] input:radio[name='"+raName+"']").is(":checked");
        if(val==false){
            alert("未选择收货地址！");
            return false;
        }else{
            return true;
        }
    }
</script>
<div class="container-fluid">
<div class="row">
    <h3>确认收货地址</h3>
    <table class="table table-striped" id="address_tab">
        <thead>
        <tr>
            <th>选择</th>
            <th>省-市-区</th>
            <th>街道</th>
            <th>收件人</th>
            <th>电话</th>
        </tr>
        </thead>
        <tbody id="address_tbody">

        <c:forEach items="${addressList}" var="address" varStatus="vs">
            <tr>
                <td><input type="radio" class="selectAddress" ${address.addrIsdefault == 1 ? "checked":""} name="selectAddress" id="selectAddressCheck"
                           value="${address.addrId}"></td>
                <td>${address.addrProvince} ${address.addrCity} ${address.addrArea}</td>
                <td>${address.addrContent}</td>
                <td>${address.addrReceiver}</td>
                <td>${address.addrTel}</td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</div>

<div class="row">
    <h3>确认订单信息</h3>

    <table class="table table-striped" id="cart_tab" style="text-align: left">
        <thead>
        <tr>
            <th>序号</th>
            <th>商品图片</th>
            <th>商品名称</th>
            <th>商品规格</th>
            <th>商品单价</th>
            <th>数量</th>
            <th>运费</th>
            <th>小计</th>
        </tr>
        </thead>
        <tbody id="cart_tbody">
        <c:set var="sumPrice" value="0"/>
        <c:forEach items="${param.goodId == null?sessionScope.car:sessionScope.payGoods}" var="s" varStatus="vs">
            <tr id="${s.trId}">
                <td>${vs.index+1}</td>
                <td><img src="${pageContext.request.contextPath}/${s.goodsPic}"
                         style="width: 40px;height: 30px"></td>
                <td>${s.goodsName}</td>
                <td>${s.contentName}</td>
                <td>${s.goodsDiscount}</td>
                <td>${s.sales }</td>
                <td>${s.goodsPostalfee}</td>
                <td>${(s.goodsDiscount * s.sales) + s.goodsPostalfee}</td>
                <c:set value="${(s.goodsDiscount * s.sales) + s.goodsPostalfee + sumPrice}" var="sumPrice"/>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</div>

<ul class="nav nav-list">
    <li class="divider"></li>
</ul>

<div class="row" style="float: right">
    <p style="font-size: 16px;margin-top: 20px;margin-right: 40px">合计（含运费）￥${sumPrice}</p>
    <input class="btn btn-default" type="button"
           onclick="commitOrder('${pageContext.request.contextPath}','${sessionScope['login'].user.userId}','${param.goodId}')"
           value="提交订单"
           style="margin-left: calc(50% - 20px);margin-top: 10px">
</div>

<script>
    function commitOrder(baseUrl, userId, goodId) {
        let carList;
        if (goodId === ''){
            carList = '<%=session.getAttribute("carJson")%>';
        }
        else{
            carList = '<%=session.getAttribute("payGoodsJson")%>';
        }
        let cl = JSON.parse(carList);
        let re = document.getElementsByClassName('selectAddress');
        let order = {
            addrId: '',
            userId: userId,
            goods: []
        }
        for (let c of re) {
            if (c.checked)
                order.addrId = c.value;
        }
        if(order.addrId === ''){
            alert("未选择收货地址！");
            return false;
        }
        let goods = [];
        for (let c of cl) {
            let good = {
                goodsId: c.goodsId,
                contentId: c.contentId,
                orderSales: c.sales,
                goodsPrice: parseFloat(c.goodsDiscount),
                goodsPostalfee: parseFloat(c.goodsPostalfee),
                orderPrice: (parseFloat(c.sales) * parseFloat(c.goodsDiscount)) + parseFloat(c.goodsPostalfee)
            }
            goods.push(good);
        }
        order.goods = goods;
        $.ajax({
            type: "PUT",
            url: baseUrl + "/order/commitOrder.action",
            data: JSON.stringify(order),
            dataType: "json",
            success: res => {
                window.location.replace(baseUrl + "/order/unPayOrder");
            }
        })
        console.log(order)
    }
</script>
</div>

